package com.rosan.installer.ui.page.main.installer.dialog.inner

import android.content.Intent
import android.net.Uri
import android.provider.Settings
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import com.rosan.installer.R
import com.rosan.installer.data.common.util.addAll
import com.rosan.installer.data.installer.repo.InstallerRepo
import com.rosan.installer.data.recycle.util.openAppPrivileged
import com.rosan.installer.ui.page.main.installer.InstallerViewAction
import com.rosan.installer.ui.page.main.installer.InstallerViewModel
import com.rosan.installer.ui.page.main.installer.dialog.DialogParams
import com.rosan.installer.ui.page.main.installer.dialog.DialogParamsType
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

@Composable
fun installSuccessDialog( // 小写开头
    installer: InstallerRepo,
    viewModel: InstallerViewModel
): DialogParams {
    val context = LocalContext.current
    val currentPackageName by viewModel.currentPackageName.collectAsState()
    val coroutineScope = rememberCoroutineScope()
    val settings = viewModel.viewSettings

    // Collect preInstallAppInfo (represents the state *before* the successful install)
    // val currentPreInstallInfo by viewModel.preInstallAppInfo.collectAsState()

    val packageName = currentPackageName ?: installer.analysisResults.firstOrNull()?.packageName ?: ""

    // Call InstallInfoDialog, passing the collected preInstallAppInfo.
    // InstallInfoDialog will now handle the logic of displaying one or two versions.
    val baseParams = installInfoDialog(
        installer = installer,
        viewModel = viewModel,
        onTitleExtraClick = {
            if (packageName.isNotEmpty()) {
                context.startActivity(
                    Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
                        .setData(Uri.fromParts("package", packageName, null))
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                )
            }
            viewModel.dispatch(InstallerViewAction.Background)
        }
    )

    // Only override the buttons
    return baseParams.copy(
        // Subtitle is now correctly generated by InstallInfoDialog
        buttons = DialogButtons(
            DialogParamsType.InstallerInstallSuccess.id
        ) {
            val list = mutableListOf<DialogButton>()
            val intent =
                if (packageName.isNotEmpty()) context.packageManager.getLaunchIntentForPackage(
                    packageName
                ) else null
            if (intent != null) {
                list.add(DialogButton(stringResource(R.string.open)) {
                    coroutineScope.launch(Dispatchers.IO) {
                        openAppPrivileged(
                            context = context,
                            config = installer.config,
                            packageName = packageName,
                            dhizukuAutoCloseSeconds = settings.autoCloseCountDown,
                            onSuccess = { viewModel.dispatch(InstallerViewAction.Close) }
                        )
                    }
                })
            }
            list.addAll(
                /*DialogButton(stringResource(R.string.previous), 2f) {
                    viewModel.dispatch(DialogViewAction.InstallPrepare)
                },*/
                DialogButton(stringResource(R.string.finish)) {
                    viewModel.dispatch(InstallerViewAction.Close)
                }
            )
            return@DialogButtons list
        }
    )
}